package com.white.loggerserver

import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.white.loggerserver.room.Record
import kotlinx.android.synthetic.main.activity_log_detail.*
import java.io.BufferedReader
import java.io.File
import java.io.FileInputStream
import java.io.InputStreamReader
import java.nio.charset.Charset
import java.text.SimpleDateFormat
import java.util.*
import kotlin.concurrent.thread

class LogDetailActivity : AppCompatActivity() , View.OnClickListener{

    companion object {
        fun active(act: Activity, item : Record) {
            val it = Intent(act, LogDetailActivity::class.java)
            it.putExtra("item", item)
            act.startActivityForResult(it, 2)
        }
    }

    private var item : Record? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_log_detail)
        supportActionBar?.let {
            it.title = "设置"
            it.setDisplayHomeAsUpEnabled(true)
        }

        item = intent.getParcelableExtra("item")
        setView()
    }

    private var sdf = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)

    private fun setView(){
        titleTv.text = item?.title
        item?.addTime?.let {
            timeTv.text = sdf.format(Date(it))
        }
        item?.path?.let {ph->
            thread {
                val f = File(ph)
                if(!f.exists()) return@thread

                val fos = BufferedReader(InputStreamReader(FileInputStream(f), Charset.forName("UTF-8")))
                val sb = StringBuilder()
                var str = ""
                while (fos.readLine()?.also { str = it } != null){
                    sb.append(str).append('\n')
                }
                runOnUiThread{
                    logContent.text = sb.toString()
                }
            }
        }
        del.setOnClickListener(this)
    }

    override fun onClick(v: View?) {

        when(v){
            del->{
                AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_delete)
                    .setTitle("删除提示")
                    .setMessage("确认删除当前日志")
                    .setPositiveButton("删除"){dialog, which->
                        val file = File(item?.path)
                        file.delete()
                        val it = Intent(this@LogDetailActivity, LogRecordListActivity::class.java)
                        it.putExtra("item", item)
                        setResult(Activity.RESULT_OK, it)
                        finish()
                    }
                    .show()
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            android.R.id.home->{
                finish()
            }
        }
        return true
    }
}